package com.jinzhi.sys.shiro;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import lombok.AllArgsConstructor;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;


public class PhoneAndVerificationCodeShiroRealm extends AuthorizingRealm {
 
    @Autowired
    private  UserService userService;
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }
 
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        PhoneAndVerificationCodeToken phoneToken=(PhoneAndVerificationCodeToken) token;
        String phone = (String) phoneToken.getPrincipal();
        EntityWrapper<UserDO> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("mobile",phone);
        UserDO userDO = userService.selectOne(entityWrapper);
        //因为没有密码，并且验证码在之前就验证了 所以我这里没有验证密码
        if(userDO == null){
            throw new UnknownAccountException();
        }
        return new SimpleAuthenticationInfo(userDO,phone,getName());
    }
 
    /**
     * 用来判断是否使用当前的 realm
     * @param var1 传入的token
     * @return true就使用，false就不使用
     */
    @Override
    public boolean supports(AuthenticationToken var1) {
        return var1 instanceof PhoneAndVerificationCodeToken;
    }
}